#!/bin/sh

cpu_arch="$(cat "/proc/cpuinfo" | grep "model name" | sed -n "1p" | awk -F ': ' '{print $2}')"
[ -z "${cpu_arch}" ] && cpu_arch="?"
cpu_cores="$(cat "/proc/cpuinfo" | grep "processor" | wc -l)"

if grep -q "bcm27xx" "/etc/openwrt_release"; then
	cpu_freq="$(expr $(vcgencmd measure_clock arm | awk -F '=' '{print $2}') / 1000000)Mhz"
elif grep -q "bcm53xx" "/etc/openwrt_release"; then
	cpu_freq="$(nvram get clkfreq | awk -F ',' '{print $1}')MHz"
elif grep -q "mvebu" "/etc/openwrt_release"; then
	cpu_freq="$(cat "/proc/cpuinfo" | grep "BogoMIPS" | sed -n "1p" | awk -F ': ' '{print $2}')MHz"
elif [ -e "/sys/devices/system/cpu/cpufreq/policy0/cpuinfo_cur_freq" ]; then
	cpu_freq="$(expr $(cat "/sys/devices/system/cpu/cpufreq/policy0/cpuinfo_cur_freq") / 1000)MHz"
fi
if [ -e "/sys/devices/system/cpu/cpufreq/policy4/cpuinfo_cur_freq" ]; then
	big_cpu_freq="$(expr $(cat "/sys/devices/system/cpu/cpufreq/policy4/cpuinfo_cur_freq") / 1000)MHz "
fi

if grep -q "bcm27xx" "/etc/openwrt_release"; then
	cpu_temp="$(vcgencmd measure_temp | awk -F '=' '{print $2}' | awk -F "'" '{print $1}')°C"
elif grep -q "ipq40xx" "/etc/openwrt_release"; then
	cpu_temp="$(awk '{printf("%.1f°C ", $0 / 1000)}' /sys/class/ieee80211/phy*/device/hwmon/hwmon*/temp1_input | awk '$1=$1')"
else
	[ -e "/sys/class/thermal/thermal_zone0/temp" ] && \
		cpu_temp="$(awk '{printf("%.1f°C", $0 / 1000)}' /sys/class/thermal/thermal_zone0/temp)"
fi

if [ -z "${cpu_freq}" ] && [ -z "${cpu_temp}" ]; then
	echo -n "${cpu_arch} x ${cpu_cores}"
elif [ -z "${cpu_temp}" ]; then
	echo -n "${cpu_arch} x ${cpu_cores} (${cpu_freq})"
elif [ -z "${cpu_freq}" ]; then
	echo -n "${cpu_arch} x ${cpu_cores} (${cpu_temp})"
else
	echo -n "${cpu_arch} x ${cpu_cores} (${big_cpu_freq}${cpu_freq}, ${cpu_temp})"
fi
